# -*- py-indent-offset: 4 -*- 
# -*- tab-width: 8 -*- 
# -*- indent-tabs-mode: nil -*-
 
from etgui import *
import CmdNo,string
from PageLayout import *
from ListEditor import AbstrListEditor,SortedListEditorMixin,ListEditor
from ListEditor import SimpleListEditor

class TestListEditor(SimpleListEditor):
	"""Items beginning with - are not deletable and shown in Italic."""
	def IsNormal(self,value):
		return value[:1]!="-"
	def DoMakeItem(self,v):
		if self.IsNormal(v):
			return TextItem(str(v))
		return TextItem(
			str(v),
			font=Font(
				gSysFont.Fid(),
				gSysFont.Size(),
				eFaceItalic
			)
		)
	def CanEdit(self,i):
		return self.IsNormal(self.items[i])
	def CanDelete(self,i):
		return self.IsNormal(self.items[i])
	def DoEdit(self,i):
		print "DoEdit"
		for item in self.items:
			print "  %s" %repr(item)
	def ListChanged(self):
		print self.items
class TestSortedListEditor(SortedListEditorMixin,TestListEditor):
	pass

class ListWithNewAction(TestListEditor):
	def CanGenerate(self,i):
		return 1
	def Generate(self):
		l=[
			"This",
			"is",
			"a",
			"generated"
			"list",
			"-I/.."
		]
		self.SetItems(l)
		self.ListChanged()
	def MakeGenerateButton(self):
		return self.NewButton("Generate...",self.Generate,self.CanGenerate,1)
	def GetActionButtonConstructors(self):
		return [self.MakeGenerateButton]


class ListWith2NewActions(ListWithNewAction):
	def CanLower(self,i):
		return self.items[i]!=string.lower(self.items[i])
	def Lower(self):
		i=self.GetSelected()
		newitem=string.lower(self.items[i])
		self.Replace(newitem)
	def MakeLowerButton(self):
		return self.NewButton("Lower...",self.Lower,self.CanLower)
	def GetActionButtonConstructors(self):
		parentlist= ListWithNewAction.GetActionButtonConstructors(self)
		return parentlist+[self.MakeLowerButton]


def MakeListEditor(editor):
	items=[
		"-I.",
		"Michael",
		"-I..",
		"Test",
		"-Test",
		"AHA",
	]
	editor.SetItems(items)
	return MakeSectionLayout(
		MakeListEditorLayout(editor)
	)


def MakeDialog():
	import sys
	panel=MakeTabSheetLayout(
		TabPage(
			"ListEditor",
			MakeListEditor(ListEditor())
		),
		TabPage(
			"Simple",
			MakeListEditor(SimpleListEditor("Test"))
		),
		TabPage(
			"SpecialItems",
			MakeListEditor(TestListEditor("Test"))
		),
		TabPage(
			"Sorted",
			MakeListEditor(TestSortedListEditor("Test"))
		),
		TabPage(
			"Action",
			MakeListEditor(ListWithNewAction("Test"))
		),
		TabPage(
			"2 Actions",
			MakeListEditor(ListWith2NewActions("Test"))
		),
	)
	dialog=PotGuiDialog(
		PotGuiModel(),
		MakeDialogWithButtons(
			panel,
			ActionButton(CmdNo.cIdOk,"OK",default=1),
			ActionButton(CmdNo.cIdCancel,"Cancel"),
		),
		"Pot GUI",
		eWinDefault
	)
	return dialog
gDialog=None
def Show():
	global gDialog
	if not gDialog:
		gDialog=MakeDialog()
	gDialog.ShowUnderMouse()
